<?php
include '../includes/cDatabase.php';
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/variables.php';
include '../includes/cPage.php';
include '../includes/cTransfer.php';
include '../includes/cDates.php';

$cPage = new page("Transfer Player");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$cTransfer = new transfer();

if(@$_GET['form'] == "selectTeam")
{
	// Form 1 has been submitted, this contains the team name
	$teamName = $_POST['teamName'];
	
	// Check this team has not exceeded the max number of transfers
	$transfersRemaining = $cTransfer->checkTeamTransfers($teamName);
	
	if (!$transfersRemaining)
	{
		echo "<BR>".$teamName." has no transfers remaining.";
		?>
		<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=nothing" method="POST">
	  		<div align="center"><table border='0'>
				<tr>
			  	<td colspan="2" align="center">
			  	<INPUT TYPE="BUTTON" VALUE="Main Menu" ONCLICK="window.location.href='http://localhost'"></td>
		 		</tr>
	  		</table></div>
		</form>
		<?php
	}
	else
	{
		$cTransfer->selectTransfer($teamName, $transfersRemaining);
	}
}

else if(@$_GET['form'] == "submitTransfer")
{
	// Form 2 has been submitted, this contains the selected team, the player out and the player in
	$playerCodeOut = $_POST['selectPlayerOut'];
	$playerCodeIn = $_POST['selectPlayerIn'];
	$week = $_POST['selectWeek'];
	$teamName = $_POST['selectTeamName'];
	$flagA = 0;
	$flagB = 0;
	
	// Check this transfer does not exceed the maximum team value
	// Check the new formation is valid
	$error = $cTransfer->checkTeam($teamName, $playerCodeIn, $playerCodeOut);
	
	if (!$error)
	{
		$cTransfer->doTransfer($teamName, $playerCodeIn, $playerCodeOut, $week);
	}
	else
	{
		echo "<BR> Error [".$error."] with transfer";
	}
	// Display the first menu where the actual team is selected to whcih the transfer will be made
	?>
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=nothing" method="POST">
	  	<div align="center"><table border='0'>
			<tr>
			  <td colspan="2" align="center">
			  <INPUT TYPE="BUTTON" VALUE="Main Menu" ONCLICK="window.location.href='http://localhost'"></td>
		 	</tr>
	  	</table></div>
	</form>
	<?php
}

else
{
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	
	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);
	
	$finfo = mysqli_fetch_fields($result);
	$numberRows = mysqli_num_rows($result);
	
	// Display the first menu where the actual team is selected to which the transfer will be made
	?>
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=selectTeam" method="POST">
	  	<table border='0'>
			<tr>
			  <td colspan="2" align="center">
	    		<select name="teamName">
				<?php
				for ($j = 0; $j < $numberRows; $j++)
				{
					$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	    		  	echo"<option value = '" .$row['teamName']. "'>" .$row['memberName']."-".$row['teamName']. "</option>";
				}
				?>
		        </select>
	    		<input name="selectMemberButton" type="submit" id="selectMember" value="Select Member"></td>
		 	</tr>
	  	</table>
	</form>
	<?php
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
?>
